package com.linh.trade;

import com.linh.trade.Trade.Direction;
import com.linh.trade.Trade.State;
import com.linh.trade.data.Point;
import com.linh.trade.valuation.Valuator;

public class Operator {

	protected Valuator valuator;

	protected double budget;
	
	protected double initialBudget;

	public Operator(Valuator valuator, double budget) {
		super();
		this.valuator = valuator;
		this.budget = budget;
		this.initialBudget=budget;
	}

	public Trade goLong(Point point, double amount) {

		Trade trade = new Trade();
		trade.setAmount(amount);
		trade.setDirection(Direction.LONG);
		trade.setState(State.OPEN);
		trade.setOpeningPoint(point);

		// deduct budget
		budget = budget - amount;
		valuator.addTrade(trade);
		return trade;
	}

	public Trade goShort(Point point, double amount) {

		Trade trade = new Trade();
		trade.setAmount(amount);
		trade.setDirection(Direction.SHORT);
		trade.setState(State.OPEN);
		trade.setOpeningPoint(point);
		
		budget = budget - amount;
		valuator.addTrade(trade);

		return trade;
	}

	public void closePosition(Trade trade, Point point) {
		trade.setState(State.CLOSE);
		trade.setClosingPoint(point);
		budget += trade.getAmount() + valuator.realizedPnL(trade);
	}

	public Valuator getValuator() {
		return valuator;
	}

	public void setValuator(Valuator valuator) {
		this.valuator = valuator;
	}

	public double getBudget() {
		return budget;
	}

}
